﻿package mortal.game.view.activeTime
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.task.guild.*;
    import mortal.mvc.core.*;

    public class ActiveTimeController extends Controller
    {
        private var _lastActivesDic:Vector.<SClientActive>;
        private var _currentActiveDic:SClientActiveMsg;

        public function ActiveTimeController()
        {
            this._lastActivesDic = new Vector.<SClientActive>;
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicActiveToClient, this.activeHandler);
            Dispatcher.addEventListener(EventName.ActiveStart, this.activesStartHandler);
            Dispatcher.addEventListener(EventName.ActiveEnd, this.activesEndHandler);
            Dispatcher.addEventListener(EventName.ActiveNotic, this.activesNoticHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.roleLevelupHandler);
            return;
        }// end function

        private function roleLevelupHandler(param1:Object) : void
        {
            var _loc_3:SClientActive = null;
            var _loc_4:Number = NaN;
            var _loc_5:int = 0;
            var _loc_2:* = ActiveTimeUtil.isInDoubleTimeByType(EActiveType._EActiveTypeGuildTask);
            if (_loc_2 && cache.role.roleInfo.level >= GameDefConfig.instance.getSystemOpenLevel(78))
            {
                GuildTaskGetWinNew.isDoubleRewards = true;
                GuildDoubleRewardHintIcon.instance.show();
                _loc_3 = ActiveTimeUtil.getSClientActive(EActiveType._EActiveTypeGuildTask);
                if (_loc_3 != null)
                {
                    _loc_4 = ClockManager.instance.nowDate.time;
                    if (_loc_4 >= _loc_3.startDt.time)
                    {
                        _loc_5 = Math.ceil((_loc_3.endDt.time - _loc_4) / 1000);
                        GuildDoubleRewardHintIcon.instance.setLeftTimeAndState(_loc_5, 2);
                    }
                    else
                    {
                        _loc_5 = Math.ceil((_loc_3.startDt.time - _loc_4) / 1000);
                        GuildDoubleRewardHintIcon.instance.setLeftTimeAndState(_loc_5, 1);
                    }
                }
            }
            return;
        }// end function

        public function get lastActivesDic() : Vector.<SClientActive>
        {
            return this._lastActivesDic;
        }// end function

        public function get currentActiveDic() : Array
        {
            var _loc_1:Array = null;
            if (this._currentActiveDic)
            {
                _loc_1 = this._currentActiveDic.actives;
            }
            else
            {
                _loc_1 = [];
            }
            return _loc_1;
        }// end function

        private function activeHandler(param1:MessageBlock) : void
        {
            var _loc_3:SClientActive = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_9:SClientActive = null;
            var _loc_10:Boolean = false;
            var _loc_11:int = 0;
            var _loc_12:int = 0;
            var _loc_13:int = 0;
            var _loc_14:Array = null;
            var _loc_15:Boolean = false;
            var _loc_16:Number = NaN;
            var _loc_17:Number = NaN;
            var _loc_2:* = param1.messageBase as SClientActiveMsg;
            this._currentActiveDic = _loc_2;
            var _loc_7:* = ClockManager.instance.nowDate.time;
            var _loc_8:* = new Vector.<SClientActive>;
            _loc_4 = 0;
            while (_loc_4 < _loc_2.actives.length)
            {
                
                _loc_9 = _loc_2.actives[_loc_4];
                _loc_10 = false;
                _loc_5 = 0;
                while (_loc_5 < _loc_8.length)
                {
                    
                    if (_loc_8[_loc_5].type == _loc_9.type)
                    {
                        _loc_10 = true;
                        _loc_12 = Math.min(Math.abs(_loc_9.noticeDt.time - _loc_7), Math.abs(_loc_9.endDt.time - _loc_7));
                        _loc_13 = Math.min(Math.abs(_loc_8[_loc_5].noticeDt.time - _loc_7), Math.abs(_loc_8[_loc_5].endDt.time - _loc_7));
                        if (_loc_12 < _loc_13)
                        {
                            _loc_8.splice(_loc_5, 1, _loc_9);
                        }
                        break;
                    }
                    _loc_5++;
                }
                if (!_loc_10)
                {
                    _loc_8.push(_loc_9);
                }
                _loc_4++;
            }
            if (this._lastActivesDic)
            {
                _loc_14 = [];
                _loc_4 = 0;
                while (_loc_4 < this._lastActivesDic.length)
                {
                    
                    _loc_15 = false;
                    _loc_5 = 0;
                    while (_loc_5 < _loc_8.length)
                    {
                        
                        if ((this._lastActivesDic[_loc_4] as SClientActive).type == (_loc_8[_loc_5] as SClientActive).type)
                        {
                            _loc_15 = true;
                            break;
                        }
                        _loc_5++;
                    }
                    if (!_loc_15)
                    {
                        _loc_14.push(this._lastActivesDic[_loc_4]);
                    }
                    _loc_4++;
                }
                _loc_4 = 0;
                while (_loc_4 < _loc_14.length)
                {
                    
                    _loc_3 = _loc_14[_loc_4] as SClientActive;
                    _loc_6 = this.checkInLastActiveTime(_loc_3.type);
                    if (_loc_6 != 0)
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ActiveEnd, _loc_14[_loc_4] as SClientActive));
                    }
                    _loc_4++;
                }
            }
            _loc_4 = 0;
            while (_loc_4 < _loc_8.length)
            {
                
                _loc_3 = _loc_8[_loc_4];
                _loc_6 = this.checkInLastActiveTime(_loc_3.type);
                _loc_16 = _loc_3.startDt.time;
                _loc_17 = _loc_3.endDt.time;
                if (_loc_7 > _loc_17)
                {
                    if (_loc_6 > 0)
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ActiveEnd, _loc_3));
                    }
                }
                else if (_loc_7 > _loc_16 && _loc_7 <= _loc_17)
                {
                    if (_loc_6 < 2)
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ActiveStart, _loc_3));
                    }
                    ClockManager.instance.addDateCall(_loc_3.endDt, this.activeEndHandler, _loc_3);
                }
                else
                {
                    if (_loc_3.noticeDt.time != _loc_3.startDt.time)
                    {
                        if (_loc_7 > _loc_3.noticeDt.time)
                        {
                            if (_loc_6 < 1)
                            {
                                Dispatcher.dispatchEvent(new DataEvent(EventName.ActiveNotic, _loc_3));
                            }
                        }
                        else
                        {
                            ClockManager.instance.addDateCall(_loc_3.noticeDt, this.activeNoticHandler, _loc_3);
                        }
                    }
                    ClockManager.instance.addDateCall(_loc_3.startDt, this.activeStartHandler, _loc_3);
                    ClockManager.instance.addDateCall(_loc_3.endDt, this.activeEndHandler, _loc_3);
                }
                _loc_4++;
            }
            this._lastActivesDic = _loc_8;
            return;
        }// end function

        private function checkInLastActiveTime(param1:int) : int
        {
            var _loc_3:SClientActive = null;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            if (!this._lastActivesDic)
            {
                return 0;
            }
            var _loc_2:int = 0;
            while (_loc_2 < this._lastActivesDic.length)
            {
                
                _loc_3 = this._lastActivesDic[_loc_2];
                if (_loc_3.type == param1)
                {
                    _loc_4 = _loc_3.startDt.time;
                    _loc_5 = _loc_3.endDt.time;
                    _loc_6 = ClockManager.instance.nowDate.time;
                    if (_loc_6 > _loc_5)
                    {
                    }
                    else
                    {
                        if (_loc_6 >= _loc_4 && _loc_6 <= _loc_5)
                        {
                            ClockManager.instance.removeDateCall(_loc_3.endDt);
                            return 2;
                        }
                        ClockManager.instance.removeDateCall(_loc_3.startDt);
                        ClockManager.instance.removeDateCall(_loc_3.endDt);
                        if (_loc_3.noticeDt.time != _loc_3.startDt.time)
                        {
                            if (_loc_6 <= _loc_3.noticeDt.time)
                            {
                                ClockManager.instance.removeDateCall(_loc_3.noticeDt);
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                }
                _loc_2++;
            }
            return 0;
        }// end function

        private function activeNoticHandler(param1:SClientActive) : void
        {
            var _loc_2:* = ClockManager.instance.nowDate.time;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ActiveNotic, param1));
            return;
        }// end function

        private function activeStartHandler(param1:SClientActive) : void
        {
            var _loc_2:* = ClockManager.instance.nowDate.time;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ActiveStart, param1));
            return;
        }// end function

        private function activeEndHandler(param1:SClientActive) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ActiveEnd, param1));
            return;
        }// end function

        private function activesStartHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2 == null)
            {
                return;
            }
            var _loc_3:* = Math.ceil((_loc_2.endDt.time - ClockManager.instance.nowDate.time) / 1000);
            if (_loc_3 <= 0)
            {
                return;
            }
            if (_loc_2.type == EActiveType._EActiveTypeGuildTask)
            {
                if (cache.guild.hasGuild && GuildTaskGetWinNew.isDoubleRewards == false && Cache.instance.role.entityInfo.level >= GameDefConfig.instance.getSystemOpenLevel(78) && cache.task.getGroupFinishCount(ETaskGroup._ETaskGroupGuild) < 10)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildTaskDoubleRewardViewShow));
                }
                GuildTaskGetWinNew.isDoubleRewards = true;
                if (Cache.instance.role.entityInfo.level >= GameDefConfig.instance.getSystemOpenLevel(78))
                {
                    GuildDoubleRewardHintIcon.instance.show();
                    GuildDoubleRewardHintIcon.instance.setLeftTimeAndState(_loc_3, 2);
                }
            }
            return;
        }// end function

        private function activesEndHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type == EActiveType._EActiveTypeGuildTask)
            {
                GuildTaskGetWinNew.isDoubleRewards = false;
                GuildDoubleRewardHintIcon.instance.hide();
                if (GameController.taskImpl.guildTaskGetWin && !GameController.taskImpl.guildTaskGetWin.isHide)
                {
                    GameController.taskImpl.guildTaskGetWin.hide();
                }
            }
            return;
        }// end function

        private function activesNoticHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2 == null)
            {
                return;
            }
            var _loc_3:* = Math.ceil((_loc_2.startDt.time - ClockManager.instance.nowDate.time) / 1000);
            if (_loc_3 <= 0)
            {
                return;
            }
            switch(_loc_2.type)
            {
                case EActiveType._EActiveTypeGuildTask:
                {
                    GuildDoubleRewardHintIcon.instance.show();
                    GuildDoubleRewardHintIcon.instance.setLeftTimeAndState(_loc_3, 1);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

    }
}
